package cn.edu.ujn.dongz023.info.project.controller;

import java.util.Date;
import java.util.List;

import cn.edu.ujn.dongz023.info.project.entity.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.ujn.dongz023.info.project.service.IProjectService;

@Controller
public class ProjectController {
    @Autowired
    private IProjectService projectService;

    @RequestMapping("/project/{id}")
    public String find(@PathVariable("id") int id, Model model) {
        Project project = projectService.findById(id);
        model.addAttribute("project", project);
        return "project";
    }

    @GetMapping("/add")
    public String toadd() {
        return "addProject";
    }

    @RequestMapping("/addProject")
    public String add(Project project) {
        project.setAddtime(new Date());
        Project add = this.projectService.add(project);
        return "redirect:findAll";
    }

    @RequestMapping("/update/{id}")
    public String update(@PathVariable("id") int id, Model model) {
        Project project = projectService.findById(id);
        model.addAttribute("project", project);
        return "updateProject";
    }

    @RequestMapping("/updateProject")
    public String update(Project project, Model model) {
        projectService.update(project);
        model.addAttribute("project", project);
        return "redirect:findAll";
    }

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Project> findAll = this.projectService.findAll();
        model.addAttribute("list", findAll);
        return "listProject";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        projectService.delete(id);
        return "redirect:findAll";
    }

}
